package lium.lbs.lbsim.activity

import android.Manifest
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.Gravity
import android.view.KeyEvent
import android.view.View
import com.google.zxing.BarcodeFormat
import com.google.zxing.ResultPoint
import com.journeyapps.barcodescanner.BarcodeCallback
import com.journeyapps.barcodescanner.BarcodeResult
import com.journeyapps.barcodescanner.DecoratedBarcodeView
import com.journeyapps.barcodescanner.DefaultDecoderFactory
import lium.lbs.lbsim.utils.ToastUtils
import com.tbruyelle.rxpermissions2.RxPermissions
import lium.lbs.lbsim.R
import lium.lbs.lbsim.utils.IAlertDialog

import java.util.Arrays

class QRCodeActivity : BaseActivity() {
    private var barcodeView: DecoratedBarcodeView? = null
    private var isLight = false
    override fun getLayoutId(): Int {  return R.layout.activity_qrcode  }

    private val callback = object : BarcodeCallback {
        override fun barcodeResult(result: BarcodeResult) {
            if (result.text == null) {
                // Prevent duplicate scans
                return
            }
            barcodeView!!.setStatusText(result.text)
            parseQRCode(result.text, Bundle())
        }
        override fun possibleResultPoints(resultPoints: List<ResultPoint>) {}
    }


    override fun initView(savedInstanceState: Bundle?) {
        setTitleWithBack("扫一扫")
        barcodeView = findViewById(R.id.barcode_scanner)
        val formats = Arrays.asList(BarcodeFormat.QR_CODE, BarcodeFormat.CODE_39)
        barcodeView!!.barcodeView.decoderFactory = DefaultDecoderFactory(formats)
        barcodeView!!.decodeContinuous(callback)
        addOnClickListeners(R.id.tv_zxing_flashlight)
        requestPermissions()
    }

    private fun requestPermissions() {
        val rxPermission = RxPermissions(this)
        rxPermission.request(
            Manifest.permission.CAMERA,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE
        )
            .subscribe { granted ->
                if (granted!!) {

                } else {
                    requestPerFailed()
                }
            }
    }

    internal var dialog: IAlertDialog? = null
    private fun requestPerFailed() {
        if (dialog == null) {
            dialog = IAlertDialog(this, IAlertDialog.LayoutStyle.THEME_WHITE_TITLE_SINGLEBUTTON, Gravity.CENTER)
            dialog!!.setMessage(resources.getString(R.string.app_name)+"需要相机权限拍摄二维码，否则无法正常使用扫码功能，请到设置开启权限允许")
            dialog!!.setPositiveMsg("确定")
            dialog!!.setPositiveOnClickListener { dialog, which ->
                finish()
            }
        }
        dialog!!.show()
    }

    override fun onClick(v: View) {
        super.onClick(v)
        when (v.id) {
            R.id.tv_zxing_flashlight -> if (!isLight) {
                barcodeView!!.setTorchOn()
                isLight = true
            } else {
                isLight = false
                barcodeView!!.setTorchOff()
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            when (requestCode) {

            }
        }

    }

    //解析二维码中的字符串
    private fun parseQRCode(result: String, bundle: Bundle) {
        ToastUtils.showToast(result + "")
    }


    override fun getNetWorkData() {

    }

    override fun onResume() {
        super.onResume()
        barcodeView!!.resume()
    }

    override fun onPause() {
        super.onPause()
        barcodeView!!.pause()
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        return barcodeView!!.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event)
    }


}
